<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
/*
 * 获取用户cookie
 */
function userCookie(){
	$userinfo = $_COOKIE['user_userinfo'];
	if(!empty($userinfo)){
		return $userinfo;
	}
}
/*
 * 获取当前网址
 */
function getURL(){
	$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
	$url = $protocol.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
	$url = str_replace(':80', '', $url);
	$url = explode('#',$url)[0];
	return $url;
}
/*
 * 设置分页样式
 * $obj 传入分页对象
 */
function pagestyle($obj=null){
	if(isset($obj)){
		$Page = $obj;
		$Page->lastSuffix=false;
		$Page->setConfig('header','&nbsp;&nbsp;[共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页]');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('last','末页');
		$Page->setConfig('first','首页');
		$Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		return $Page;
	}
}
/*
 * 显示分页limit返回值
 */
function limitbakstr($obj=null){
	if(isset($obj)){
		return $obj->firstRow.','.$obj->listRows;
	}
}

/*
 * 字符串截取
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
	if(function_exists("mb_substr")){
		$slice= mb_substr($str, $start, $length, $charset);
	}elseif(function_exists('iconv_substr')) {
		$slice= iconv_substr($str,$start,$length,$charset);
	}else{
		$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
		$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
		$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
		$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
		preg_match_all($re[$charset], $str, $match);
		$slice = join("",array_slice($match[0], $start, $length));
	}
	$fix='';
	if(strlen($slice) < strlen($str)){
		$fix='...';
	}
	return $suffix ? $slice.$fix : $slice;
}

/*
 * 调用API
 */
function request_post($fieldarrayPost,$url=null){
	$ch = curl_init();//初始化curl
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldarrayPost);
	$data = curl_exec($ch);//运行curl
	curl_close($ch);
	return $data;
}

/*
 * curl get方式
 */
function request_get($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	$output = curl_exec($ch);
	curl_close($ch);
	return json_decode($output,true);
	//return $output;
}

/*
 * curl post方式
 */
function request_post1($url,$post_data){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	// post数据
	curl_setopt($ch, CURLOPT_POST, 1);
	// post的变量
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	$output = curl_exec($ch);
	curl_close($ch);
	//返回获得的数据
	return json_decode($output,true);
}

/*
 * 礼物
 */
function gift(){
	$giftname = array(
		array('name'=>'掌声','num'=>1),
		array('name'=>'魔法棒','num'=>50),
		array('name'=>'棒棒糖','num'=>50),
		array('name'=>'拥抱','num'=>100),
		array('name'=>'钻戒','num'=>200),
		array('name'=>'蓝玫瑰','num'=>100),
		array('name'=>'巧克力','num'=>50),
		array('name'=>'我爱你','num'=>200),
		array('name'=>'丘比特','num'=>100),
		array('name'=>'表白','num'=>100),
		array('name'=>'皇冠','num'=>200),
		array('name'=>'红宝石','num'=>200),
		array('name'=>'香水','num'=>50),
		array('name'=>'小车','num'=>200),
		array('name'=>'跑车','num'=>200),
		array('name'=>'轮船','num'=>200)
	);
	return $giftname;
}

/*根据IP获取地区*/
function iptoarea(){
	$url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip();
	$r = request_get($url);
	return $r['data'];
}

?>